<?php
include '../template.php';
include 'blastparser.php';
include 'blast_fun.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include '../data/peptideformat.php';
include '../util/dbutil.php';
include '../util/pageutil.php';
getTop("..");
?>


&nbsp;<br />
<?php
	if(!isset($_POST['blast'])&&!isset($_GET['page'])) {
		die('Please input peptide sequence!');
	}

        $wd = "./";

	if (isset($_POST['blast'])) { // first
		$seq = $_POST['seq'];
                $seq = blastseqpack($seq);

		$ecut = $_POST['evalue'];
                $org = $_POST['org'];
                $ref = $_POST['ref'];

		// by default we show first page
		$pageNum = 1;
	} else { //if(isset($_GET['page']))
                $seq = $_GET['seq'];
                $ecut = $_GET['evalue'];
		$pageNum = $_GET['page'];
                $org = $_GET['org'];
                $ref = $_GET['ref'];
                $blastres = $_GET['blastres'];
	}

        if (strlen($seq)<5) {
                die("Please input a query protein sequence with length &ge;5");
        }

        if (isset($blastres)) {
            $data = unserializeFromDb($blastres);
            if (count($data)==1)
                $hits = $data[0];
        }

        if (!isset($hits)) {
            $blastout = run_blast_nr_peptide($wd,$seq,$ecut,$org,$ref);
            $hits = blastparse($blastout);
            unlink($blastout);
            $ip = getRealIpAddr();
            $blastres = str_replace('/', '', $blastout).'.'.$ip.'.'.time();
            serializeToDb($blastres,$hits);
        }

        $getref = "&org=$org&ref=$ref";

	$get = "evalue=$ecut&org=$org&ref=$ref&blastres=$blastres&seq=$seq";

//        print_r($hits);

	// how many rows to show per page
	$rowsPerPage = 10;

	// counting the offset
	$offset = ($pageNum - 1) * $rowsPerPage;

	$count = count($hits);
	if ($count==0) {
		echo "&nbsp;<br />";
		echo "Ooops... No significant hit was found (E-value&le;$ecut).<br/> ".
                     "Please increase the E-value threshold (i.e. less stringent) if possible, and try again.";
	} else { //>0
		//$ids = array_keys($hits);

                echo "<table width=\"100%\" border=\"0\"><tr valign=\"top\">";
                echo "<td><font size=\"3\"><b>$count phosphopeptides matched (E-value&le;$ecut)</b></font></td>";
                echo "<td align=\"right\" >";
                echo "<form method=\"post\" action=\"./pepbrowser_action.php\"> ";
                echo "<input type=\"hidden\" name=\"seq\" value=\"$seq\">";
                echo "<input type=\"hidden\" name=\"evalue\" value=\"$ecut\">";
                echo "<input type=\"hidden\" name=\"org\" value=\"$org\">";
                echo "<input type=\"hidden\" name=\"ref\" value=\"$ref\">";
                echo "<input type=\"hidden\" name=\"blastres\" value=\"$blastres\">";
                echo "<input name=\"blast\" type=\"submit\" id=\blast\" value=\"Send to Phosphopeptide BLAST Browser\">";
                echo "</form>";
                echo "</td></tr></table>";

		echo "<table border=\"1\" width=\"100%\">";
		echo "<tr><td align=\"center\"><b>organism</b></td><td align=\"center\"><b>Protein</b></td><td align=\"center\"><b>E-value</b></td><td align=\"center\"><b>Alignment<b></td></tr>";
		//foreach($hits as $gi=>$evalue) {
		for ($i=$offset; $i<$count && $i<$offset+$rowsPerPage; $i++) {
		//for ($i=0; $i<4 && $i<2; $i++) {
                //foreach ($hits as $hit) {
                        $hit = $hits[$i];
			$nrseq = $hit["nrseq"];
                        $protein = $hit["protein"];
                        $xref = implode('&nbsp;&nbsp;', getXref($protein));
                        $pepLocation = $hit["location"];
			$evalue = $hit["evalue"];
                        $queryseq = $hit["queryseq"];
                        $querystart = $hit["querystart"];
                        $queryend = $hit["queryend"];
                        $matchseq = $hit["matchseq"];
                        $sbjctseq = $hit["sbjctseq"];
                        $sbjctstart = $hit["sbjctstart"];
                        $sbjctend = $hit["sbjctend"];
                        $sites = $hit['sites'];
			$query  = "SELECT annotation, latin FROM protein, organism WHERE protein.organism=organism.label AND protein.id=$protein;";
			$result = mysql_query($query) or die($query.'--Error, query failed');
			while ($row = mysql_fetch_array($result)) {
				//echo "<tr><td><a href=\"../data/peptide.php?id=$id\">{$row['sequence']}</a>";
                                echo "<tr>";
                                echo "<td><i>{$row['latin']}</i></td>";
				echo "<td><a href=\"../data/protein.php?id=$protein$getref\">".$xref."<br/>".$row['annotation']."</a></td>";
				echo "<td>$evalue</td>";
                                echo "<td><table>";

                                echo "<tr><td>Query:</td><td>".($querystart+1)."</td>";
                                echo "<td><font face=\"Courier New, Courier, Monaco, ProFont, Monotype Corsiva, Base Monospace\">".formatquerypep($queryseq,$sbjctseq,$sites)."</font></td>";
                                echo "<td>".($queryend+1)."</td></tr>";

                                echo "<tr><td>&nbsp;</td><td>&nbsp;</td>";
                                echo "<td><font face=\"Courier New, Courier, Monaco, ProFont, Monotype Corsiva, Base Monospace\">".str_replace(" ","&nbsp;",$matchseq)."</font></td>";
                                echo "<td>&nbsp;</td></tr>";


                                $pep_html = str_replace('#', '%23', $nrseq);
                                $a = "<a  href=\"../data/peptide.php?pnrseq=$pep_html&pro=$protein&loc=$pepLocation$getref\">";
                                echo "<tr><td>Sbjct:</td><td>".($pepLocation+$sbjctstart+1)."</td>";
                                echo "<td><font face=\"Courier New, Courier, Monaco, ProFont, Monotype Corsiva, Base Monospace\">".formatpep_link($sbjctseq,$a,$sites,"../data",$getref)."</font></td>";
                                echo "<td>".($pepLocation+$sbjctend+1)."</td></tr>";

                                echo "</table></td>";
                                echo "</tr>";

			}
		}
		echo "</table>";
	}

	// how many pages we have when using paging?
	$maxPage = ceil($count/$rowsPerPage);
        $self = $_SERVER['PHP_SELF'];
        printPageNav($maxPage,"$self?$get&",$pageNum);

        if ($count>0) {
          //$blastouturl = "./tmp/".basename($blastout);
          echo "<table width=\"100%\"><tr><td align=\"right\"><table><tr>";
          //echo "<td><form method=\"post\" action=\"$blastouturl\"><input name=\"res\" type=\"submit\" id=\"res\" value=\"Get BLAST alignment file\"></form ></td>";
          echo "<td align=\"right\"><form method=\"post\" action=\".\"><input name=\"new\" type=\"submit\" id=\"new\" value=\"New inquiry\"></form></td></tr></table>";
          echo "</td></tr></table>";
        }

	include '../dbconfig/closedb.php';
?>
	

<?php getBottom(); ?>
